﻿//------------------------------------------------------------------------------
// <copyright file="Taskbar.cs" company="Robert MacLean">
//      Copyright © Robert MacLean.  All Rights Reserved.
// </copyright>
//
//  Details on this see: http://rule18.codeplex.com
//  This code released under the terms of the 
//  Microsoft Reciprocal License (MS-RL, http://opensource.org/licenses/ms-rl.html.)
//------------------------------------------------------------------------------
namespace Rule18.Code
{
    using System.Threading;
    using Microsoft.WindowsAPICodePack.Taskbar;
    using System.Diagnostics.Contracts;

    /// <summary>
    /// Provides some basic functionality for working with the Win7 taskbar.
    /// </summary>
    public static class Taskbar  
    {
        /// <summary>
        /// The time to flash the task bar in milliseconds
        /// </summary>
        internal const int flashDelay = 300;

        /// <summary>
        /// The Win7 taskbar
        /// </summary>
        internal static TaskbarManager WindowsTaskbar = TaskbarManager.Instance;

        /// <summary>
        /// Flashes the taskbar.
        /// </summary>
        /// <param name="state">The state.</param>
        private static void FlashTaskbar(TaskbarProgressBarState state)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(FlashTaskbarThreaded), state);
        }

        /// <summary>
        /// Flashes the taskbar threaded.
        /// </summary>
        /// <param name="stateInfo">The state info.</param>
        private static void FlashTaskbarThreaded(object stateInfo)
        {
            Contract.Requires(stateInfo != null);
            Contract.Requires(stateInfo is TaskbarProgressBarState);
            Contract.Requires(flashDelay > 0);

            WindowsTaskbar.SetProgressState((TaskbarProgressBarState)stateInfo);
            WindowsTaskbar.SetProgressValue(1, 1);
            Thread.Sleep(flashDelay);
            WindowsTaskbar.SetProgressState(TaskbarProgressBarState.NoProgress);
        }

        /// <summary>
        /// Flashes the taskbar red.
        /// </summary>
        public static void FlashTaskbarRed()
        {
            FlashTaskbar(TaskbarProgressBarState.Error);
        }

        /// <summary>
        /// Flashes the taskbar green.
        /// </summary>
        public static void FlashTaskbarGreen()
        {
            FlashTaskbar(TaskbarProgressBarState.Normal);
        }

        /// <summary>
        /// Flashes the taskbar yellow.
        /// </summary>
        public static void FlashTaskbarYellow()
        {
            FlashTaskbar(TaskbarProgressBarState.Paused);
        }
    }
}
